export default{
    namespaced: true,
    state:{
        name:"product模块",
        products:[]
    },
    mutations:{
        //设置产品数据
        SET_PRODUCT(state,payload){
            console.log(payload);
            state.products=payload
            
            console.log(state);
        },
        //减少库存
        DECREASE_INVENTORY(state,payload){
            //根据传入的数据，查找products中是否存在，如果存在，数量-1

            let target =state.products.find(item => item.id === payload.id)
            // console.log(target);
            target.inventory--
        },
        //增加库存
        INCREASE_INVENTORY(state,payload){
            let target =state.products.find(item => item.id === payload.id)
            // console.log(target);
            target.inventory++
        }
    },
    actions:{
        //获取购物车数据
        FETCH_PRODUCT({commit}){
            fetch('http://chst.vip/data/index.json')
            .then(body => body.json())
            .then(res => {
                console.log(res);
                commit("SET_PRODUCT",res.data)
            })
        }

    },
    getters:{},

}